<?

/**
 *	media advert module
 *	smart module that communicates with a defined (dynamic or db) objects or global paramaters
 *	should display banners, commercials..
 *	
 *	@author		pieter van peteghem <pieter@onedotonly.be>
 *	@copyright	BSD License, 2010 oneDot only <we@onedotonly.be>
 *	@date		2 dec 2010
 *	@package	engine modules
 *	@todo		upload swf / swf rendering
 */

class MediaAdvert extends MOD {


	/**
	 *	define output, add javascript / css
	 *	@param		object
	 *	@return		html object
	 */
	 
	public function output() {
		global $Object;
		
		$this->addclass( $this->action );
		
		if( $this->action )
			$nodes = $this->{ "filter_" . $this->action }();

		if( $nodes ){
			if( is_array( $nodes )) $nodes = "<ul>" . implode( "\n", $nodes ) . "</ul>";
			return $this->parse( $nodes ) . $this->render_js();
		}	
	}
	
#	Swithcboard has to regulate the right adverts at the right place.
#	Statements are:
#	1. Relation (should it be here)
#	2. Priority (visibility amount)
#	3. Randomness (mix above statements)
#	4. Other media types (Google Adwords snippet, Charlatan Banner, ... )
	function filter_switchboard() {
		global $DB, $Object;
		
		// depth = ['global','city','location']
		// select advert where parentid in depth and depths lower and not in clicked order by rank desc, random limit 1
		
		//print_r( $DB->query_direct( "select ID from db_media where type=42 && ID in( select ref1.objid from glue_object as ref1 where ref1.objtype = 42 and parentid in ( select ref2.ID from glue_object ) )" ))
		/*select * from db_media where active && type=42 && ID in( select ref1.objid from glue_object as ref1 where ref1.objtype )*/
	
		$Advert = $DB->get_object( "media.advert", "active order by rand()" );
	
		return "<img src='/public.img?id={$Advert->id}&sze={$this->vars->sze}' alt='{$Advert->get('name')}' /><i>Reclame</i>";
		
		/* 
		

#		Relation
		if( $rel = $this->vars->relation
			&& ( $Object->type == $rel || $Object->ref_object( $rel ))) {
		
			$Object->reference(  )
		}
		*/
	}


	
	/**
	 *	most simple advert
	 *	@param		int	- id of advert
	 *	@param		string - size of image
	 * 	@param		url
	 *	@return		html object - image
	 */
	 
	function filter_single()	{
		global $Engine;
		
		$img = "<img src='/public.img?id=" . $this->vars->id . "&sze=" . $this->vars->sze . "' />";
		if( $this->vars->url )
			$img	= "<a href='" . $this->vars->url . "' target='_blank'>" . $img . "</a>";

	}
	
	

	/**
	 *	advert connected to object
	 *	@param		string	imgsize
	 *	@return		html object - image
	 */
	
	function filter_object() {
		global $Object, $DB, $Engine;
		
		// connected adverts
		if( $adverts 	= $DB->reference( $Object->type, (int) $Object->get( 'ID' ), 'media.advert' )) {
			$Advert		= new OBJECT( 'media.advert', (int) $adverts[ array_rand( $adverts, 1 ) ]);
		
			// ouput (with/without url)
			$img 		= "<img src='/public.img?id=" . $Advert->get( 'ID' ) . "&sze=" . $this->vars->sze . "' alt='" . $Advert->get( 'name' ) . "' />";
			if( $Advert->get( 'url' ))
				$img	= "<a href='" . $Advert->get( 'url' ) . "' target='_blank' title='" . $Advert->get( 'name' ) . "'>" . $img . "</a>";
		
			return $img;
		}
	}
	
	
	
	/**
	 * 	global adverts
	 *	adverts that are connected to the website
	 *	@param		string	imgsize
	 *	@return		html object - image
	 */
	 
	function filter_global() {
		global $DB;
		
		// all global adverts
		$adverts	= $DB->filter( 'media.advert', 'active = 1 AND meta LIKE "%global%"' );
		
		// make array for objects
		foreach( $adverts as $advert ) {
			
			if( $this->vars->rank )
				for( $i = 0; $i<= $advert[ 'rank' ]; $i++ ) { $ads[] 	= $advert[ 'ID' ]; }
			else
				$ads[]  = $advert[ 'ID' ];
		}	
		
		// new media.advert object					
		$Advert		= new OBJECT( 'media.advert', (int) $ads[ array_rand( $ads, 1 ) ]);
		
		// ouput (with/without url)
		$img 		= "<img src='/public.img?id=" . $Advert->get( 'ID' ) . "&sze=" . $this->vars->sze . "' alt='" . $Advert->get( 'name' ) . "' />";
		if( $Advert->get( 'url' ))
			$img	= "<a href='" . $Advert->get( 'url' ) . "' target='_blank' title='" . $Advert->get( 'name' ) . "'>" . $img . "</a>";
		
		return $img;
	}
	
	
	/**
	 * 	Google adverts
	 *	Google Adsense ad space
	 */
	function filter_adsense() {
		global $DB;
		
	
		return "<script type='text/javascript'><!--
			google_ad_client = 'ca-pub-6514182508523631';
			/* Cafeplan general */
			google_ad_slot = '{$this->vars->id}';
			google_ad_width = {$this->vars->width};
			google_ad_height ={$this->vars->height};
		//--></script>
		<script type='text/javascript' src='http://pagead2.googlesyndication.com/pagead/show_ads.js'></script>";

	}
	
	/**
	 *	reference by object
	 *	get adverts by object connected to active Object
	 */
	 
	function filter_reference() {
	 	global $DB, $Object, $Engine;
	 	
	 	if( $subtypes	= $DB->reference( $Object->type, (int) $Object->id, $this->vars->type, (( $this->vars->id ) ? (int) $this->vars->id : '' ), 'object' )) {
		 	foreach( $subtypes as $sub ) {
		 		if( $adverts = $DB->reference( $this->vars->type, (int) $sub, 'media.advert' )) {
	 				$Advert		= new OBJECT( 'media.advert', (int) $adverts[ array_rand( $adverts, 1 ) ]);
			
					// ouput (with/without url)
					$img 		= "<img src='/public.img?id=" . $Advert->get( 'ID' ) . "&sze=" . $this->vars->sze . "' alt='" . $Advert->get( 'name' ) . "' />";
					if( $Advert->get( 'url' ))
						$img	= "<a href='" . $Advert->get( 'url' ) . "' target='_blank' title='" . $Advert->get( 'name' ) . "'>" . $img . "</a>";
		
					return $img;
				}
	 		}	
	 	} 
	}
	
	
	
	/**
	 *	complicated filter
	 *	checks for all of the above
	 */
	 
	function filter_complicated() {
		global $DB, $Object, $Engine;
		
		if( $Object )
			$img = $this->filter_object(); 
			
		if( !$img && $this->vars->type )
			$img = $this->filter_reference();
			
		if( !$img && $this->vars->id )
			$img = $this->filter_single();
			
		if( !$img )
			$img = $this->filter_global();
			
		return $img;
		
	}
	
	
	
	
}

?>